package com.kaishengit.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.kaishengit.pojo.Linkman;
import com.kaishengit.pojo.Linkrecord;
import com.kaishengit.pojo.Project;
import com.kaishengit.pojo.User;
import com.kaishengit.service.LinkmanService;
import com.kaishengit.service.LinkrecordService;

@Controller
@Results({
	@Result(name="addSuccess",location="linkman.action",type="redirectAction"),
	@Result(name="editlinkmanSuccess",location="linkman.action",type="redirectAction"),
	@Result(name="delSuccess",location="linkman.action",type="redirectAction"),
	@Result(name="saveRecordSuccess",location="linkman.action",type="redirectAction")
	
})	
public class LinkmanAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private LinkmanService linkmanService;
	private LinkrecordService linkrecordService;
	
	private String id;
	private User user;
	private Project project;
	
	private Linkman linkman;
	private Linkrecord linkrecord;
	private List<Linkman> linkmanList;
	private List<Linkrecord> linkrecordList;
	
	@Override
	@Action("linkman")
	public String execute() throws Exception {
		
		project=(Project)getFromSession("project");
		id=(String) getFromSession("id");
		linkmanList=linkmanService.findAll(project);
		linkrecordList=linkrecordService.finAll();
		return SUCCESS;
	}
	
	@Action("findlinkman")
	public String findLinkman(){
		sendJson(linkmanService.findAjaxById(linkman.getId()));
		linkmanService.findAjaxById(id);
		return null;
	}
	
	@Action("addlinkman")
	public String toaddLinkman(){
		return SUCCESS;
	}
	
	@Action("savelinkman")
	public String add(){
		
		project=(Project) getFromSession("project");
		linkmanService.save(linkman,project);
		
		return "addSuccess";
	}
	
	@Action("dellinkman")
	public String dellinkman(){
		linkman=linkmanService.findById(id);
		System.out.println(id);
		linkmanService.delLinkman(linkman);
		return "delSuccess";
	}
	
	@Action("editlinkman")
	public String edit(){
		id=linkman.getId();
		linkmanService.update(linkman,id);
		return "editlinkmanSuccess";
	}
	
	
	@Action("toeditlinkman")
	public String toedit(){
		System.out.println(id);
		linkman=linkmanService.findById(id);
		
		return SUCCESS;
	}
	
	
	
	@Action("findallrecord")
	public String findAllrecord(){
		user=(User) getFromSession("user");
		linkman=linkmanService.findById(id);
		linkrecordList=linkrecordService.finAllRecord(linkman);
		System.out.println(linkrecordList.size());
		return SUCCESS;
	}
	@Action("saverecord")
	public String saveRecord(){
		linkman=linkmanService.findById(id);
		linkrecordService.save(linkrecord,(User) getFromSession("user"),linkman);
		return "saveRecordSuccess";
	}

	@Autowired
	public void setLinkmanService(LinkmanService linkmanService) {
		this.linkmanService = linkmanService;
	}

	@Autowired
	public void setLinkrecordService(LinkrecordService linkrecordService) {
		this.linkrecordService = linkrecordService;
	}
	
	

	public List<Linkman> getLinkmanList() {
		return linkmanList;
	}

	public void setLinkmanList(List<Linkman> linkmanList) {
		this.linkmanList = linkmanList;
	}

	public List<Linkrecord> getLinkrecordList() {
		return linkrecordList;
	}

	public void setLinkrecordList(List<Linkrecord> linkrecordList) {
		this.linkrecordList = linkrecordList;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public User getUser() {
		return user;
	}
	/*public void setUser(User user) {
		this.user = user;
	}*/

	public Linkman getLinkman() {
		return linkman;
	}

	public void setLinkman(Linkman linkman) {
		this.linkman = linkman;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public Linkrecord getLinkrecord() {
		return linkrecord;
	}

	public void setLinkrecord(Linkrecord linkrecord) {
		this.linkrecord = linkrecord;
	}
	
	
}
